//
//  FXStatusCell.swift
//  微博(Swift)
//
//  Created by V on 16/1/2.
//  Copyright © 2016年 V. All rights reserved.
//

import UIKit
import SnapKit

class FXStatusCell: UITableViewCell {
    
    // toolBar 的顶部约束
    var toolBarTopConstraint: Constraint?

    
    // 当前视图引用的视图模型(里面有模型数据)
    var statusViewModel: FXStatusViewModel?{
        didSet{
            // 设置 3 个视图的数据
            statusOriginalView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            // 转发微博需要进行判断时否有
            // 如果更新约束是在三个控件之前，必须选把之前对应的约束给卸载掉，再添加约束
            toolBarTopConstraint?.uninstall()
            if statusViewModel?.status?.retweeted_status != nil {
                // 尽管默认就是 false ，但是考虑到 cell 重用的问题，这里需要重新设置一下
                statusRetweetView.hidden = false
                statusRetweetView.statusViewModel = statusViewModel
                
                // 如果有转发微博，跟新下 toolBar 的顶部约束
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
                })
            }else{
                statusRetweetView.hidden = true
                
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    toolBarTopConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
            }
            statusRetweetView.statusViewModel = statusViewModel
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    // MARK: - 懒加载
    // 视图
    private lazy var statusOriginalView: FXStatusOriginalView = FXStatusOriginalView()
    // 转发 评论 赞 工具栏
    private lazy var statusToolBar: FXStatusToolBar = FXStatusToolBar()
    // 转发微博的视图
    private lazy var statusRetweetView: FXStatusRetweetView = FXStatusRetweetView()
    
    
    
    // 视图
    private func setupUI(){
//        contentView.backgroundColor = UIColor.lightGrayColor()
        // 添加原创微博视图
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusToolBar)
        contentView.addSubview(statusRetweetView)
        
        // 原创微博添加约束
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView)
            make.trailing.equalTo(contentView)
            
        }
        
        // 转发微博视图的约束
        statusRetweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        // 工具栏约束
        statusToolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        
        
        // 关于自动行高的约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(statusToolBar.snp_bottom)
            make.leading.equalTo(self)
        }
    }

}
